﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AtlantisRevenge.GamePlay
{
    public class Fleet: Ship
    {
        TimeSpan fireTime;
        public bool Fire = false;
        public bool Selected = false;

        bool OffScreen = false;

        public Fleet()
        {

            DieTime = TimeSpan.FromSeconds(0.35);
            fireTime = TimeSpan.FromSeconds(2);

            Initialize();
        }

        public override void Initialize()
        {
            Layer = 0.9f;
            HitsLeft = 3;
            Speed = FromPixelPerLoop(1.5f);
            BulletColor = Color.Red;
            Texture = shipTexture;
            Animated = false;
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (Status == ObjectStatus.Active)
            {
                fireTime = fireTime.Subtract(gameTime.ElapsedGameTime);
                
                if (fireTime.TotalSeconds <= 0 && Selected && Fire)
                {
                    Main.Add(new Bullet(this));
                    fireTime = TimeSpan.FromSeconds(2);
                }

                    if (LookRight)
                    {
                        Velocity = new Vector2(FromPixelPerLoop(Speed * 0.03f), 0);
                    }
                    else
                    {
                        Velocity = new Vector2((FromPixelPerLoop(Speed * 0.03f)* -1), 0);
                    }

                    if (Selected)
                    {
                        Color = Color.Yellow;
                    }
                    else
                    {
                        Color = Color.White;
                    }

                if (Scale > 0.6f)
                {

                    if (Position.X > Window.ClientBounds.Width + Texture.Width)
                    {
                        OffScreen = true;
                        LookRight = false;
                    }
                    else if (Position.X < 0 - Texture.Width)
                    {
                        OffScreen = true;
                        LookRight = true;
                    }
                    else
                    {
                        OffScreen = false;
                    }

                    if (OffScreen)
                    {
                        Layer = Layer - 0.1f;
                        Scale = Scale - 0.1f;

                        Console.WriteLine(Scale.ToString());
                        Console.WriteLine(Layer.ToString());

                    }

                }



            }

            base.Update(gameTime);
        }

        #region Static Region

        public static Texture2D shipTexture;

        public static Fleet GenerateShip(Viewport viewport, Vector2 P1, Vector2 P2, Vector2 P3, Vector2 P4)
        {
            Random r = new Random();
            Fleet e = new Fleet();

            Vector2 line1Position;
            Vector2 line2Position;
            Vector2 line3Position;
            Vector2 line4Position;

            line1Position = P1;
            line2Position = P2;
            line3Position = P3;
            line4Position = P4;

            Random RandomLine = new Random();
            int lineSpawn = 0;

            lineSpawn = RandomLine.Next(1, 5);

            switch (lineSpawn)
            {
                case 1:
                    e.Position= new Vector2(0, line1Position.Y);
                    break;

                case 2:
                    e.Position = new Vector2(0, line2Position.Y);
                    break;

                case 3:
                    e.Position = new Vector2(0, line3Position.Y);
                    break;

                case 4:
                    e.Position = new Vector2(0, line4Position.Y);
                    break;

                case 5:
                    lineSpawn = RandomLine.Next(1, 5);;
                    break;
            }

            return e;

        }// generate new Ship in random guide line

        #endregion
    }
}